package com.zjcode.cdcer.controller;

import com.chinatechstar.component.commons.controller.BaseController;
import com.chinatechstar.component.commons.result.ListResult;
import com.chinatechstar.component.commons.result.ResultBuilder;
import com.chinatechstar.component.commons.supports.PageData;
import com.zjcode.cdcer.model.CityEntity;
import com.zjcode.cdcer.model.basic.JsonObj;
import com.zjcode.cdcer.service.TkmSysOrgService;
import com.zjcode.cdcer.service.impl.SequenceService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName:TkmSysOrgController
 * @功能：机构管理
 * @author：wlei
 * @日期：2022/8/5-04:05
 **/
@RestController
@Slf4j
@RequestMapping("/v1/orgsetting/")
public class TkmSysOrgController extends BaseController {

    @Autowired
    private TkmSysOrgService tkmSysOrgService;

    @Autowired
    private SequenceService sequenceService;


    @PostMapping("listAllData")
    @ApiOperation("获取所有的机构数据")
    public ListResult<Object> listAllData(@RequestBody PageData pageData) {
        return ResultBuilder.buildListSuccess(tkmSysOrgService.listAllData(pageData));
    }

    @GetMapping("getAllProvince")
    @ApiOperation("获取所有的省份数据")
    public ListResult<Object> getAllProvince() {
        return ResultBuilder.buildListSuccess(tkmSysOrgService.getAllProvince());
    }

    @GetMapping("getAllProvinceCity")
    @ApiOperation("获取所有的省下面城市数据")
    public ListResult<Object> getAllProvinceCity() {
        PageData pageData = getPageData();
        return ResultBuilder.buildListSuccess(tkmSysOrgService.getAllProvinceCity(pageData));
    }

    @GetMapping("getDistrictAndStreet")
    @ApiOperation("获取所有的区街道信息")
    public ListResult<Object> getDistrictAndStreet() {
        PageData pageData = getPageData();
        return ResultBuilder.buildListSuccess(tkmSysOrgService.getDistrictAndStreet(pageData));
    }

    @PostMapping("exportOrgData")
    @ApiOperation("获取所有的区街道信息")
    public JsonObj exportOrgData(@RequestBody PageData pageData) {
        List<LinkedHashMap<String, Object>> list = tkmSysOrgService.exportOrgData(
                pageData);
        Map<String, Object> map = new HashedMap<>();
        map.put("data", list);
        return JsonObj.success(map);
    }


    @GetMapping("/getBindVillagesList")
    @ApiOperation("绑定村：列表查询，获取区县下的镇及村的列表")
    public ListResult<Object> getBindVillagesList() {
        // 仅传参：areaCode（区县code）【注：当机构的区县code空，传市code的值】
        PageData pageData=this.getPageData();
        // 区县编号
        String areaCode = pageData.getString("areaCode");
        // 机构主键id
        List<CityEntity> city = sequenceService.getBindVillagesList(areaCode);
        ListResult<Object> objectListResult = ResultBuilder.buildListSuccess(city);
        objectListResult.setSuccess(true);
        return objectListResult;
    }

    @PostMapping("saveBindList")
    @ApiOperation("绑定村：保存")
    public JsonObj saveBindList() {
        // 仅传参：id（机构id）、villageCodes（村编号集合）
        // 每次绑定，保存筛查年度（仅年份yyyy）
        return tkmSysOrgService.saveBindList(getPageData());
    }


    @GetMapping("echoBindCodes")
    @ApiOperation("绑定村：回显绑定村集合")
    public JsonObj echoBindCodes() {
        // 仅传参：id（机构id）
        return tkmSysOrgService.echoBindCodes(getPageData());
    }

    @GetMapping("/lock")
    @ApiOperation("锁定")
    public JsonObj lockOrg(@RequestParam String id) {
        // 锁定的机构，不能操作“绑定村”
        return tkmSysOrgService.lockOrg(id);
    }
    @GetMapping("/unlock")
    @ApiOperation("解锁")
    public JsonObj unlock(@RequestParam String id) {
        return tkmSysOrgService.unlock(id);
    }


    @PostMapping("setAccount")
    @ApiOperation("设置账号和密码")
    public JsonObj setAccount(@RequestBody PageData pageData) {
        return tkmSysOrgService.setAccount(pageData);
    }

}
